package com.terrier.ui.controls
{
    import com.terrier.ui.events.WxButtonEvent;

    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.utils.Dictionary;

    /**
     * 当被选中的按钮改变时调度
     * @eventType flash.events.Event.CHANGE
     */
    [Event(name = "change", type = "flash.events.Event")]

    /**
     * 单选按钮组, 确保每次只有一个按钮被选中
     * @author Jarvis.weng
     */
    public class WxCheckButtonGroup extends EventDispatcher
    {
        private var _btnDataDict:Dictionary = new Dictionary();

        private var _btnList:Array = [];

        private var _currentBtn:WxCheckButton;

        /**
         * 构造函数
         */
        public function WxCheckButtonGroup()
        {
            // nothing to do.
        }

        /**
         * 当前被选中的数据
         * @return
         */
        public function get currentData():*
        {
            return _btnDataDict[_currentBtn];
        }

        /**
         * 当前被选中的按钮
         * @return
         */
        public function get currentButton():WxCheckButton
        {
            return _currentBtn;
        }

        /**
         * 选中索引位置的按钮
         * @param index 索引值
         */
        public function selectButtonAt(index:int):void
        {
            selectButton(_btnList[index]);
        }

        /**
         * 获取指定索引位置的按钮
         * @param index 索引值
         * @return
         */
        public function getButtonAt(index:int):WxCheckButton
        {
            return _btnList[index];
        }

        /**
         * 选中一个按钮
         * @param button
         */
        public function selectButton(button:WxCheckButton):void
        {
            if (_currentBtn == button)
            {
                _currentBtn.selected = true;
            }
            else
            {
                if (_currentBtn != null)
                {
                    _currentBtn.selected = false;
                }
                _currentBtn = button;
                if (_currentBtn.selected == false)
                {
                    _currentBtn.selected = true;
                }

                dispatchEvent(new Event(Event.CHANGE));
            }
        }

        /**
         * 按钮的数量
         * @return
         */
        public function get numButtons():int
        {
            return _btnList.length;
        }

        /**
         * 添加按钮和对应的数据
         * @param button 要添加的按钮
         * @param data 与按钮对应的数据
         */
        public function addButtonAndData(button:WxCheckButton, data:*):void
        {
            _btnDataDict[button] = data;

            var index:int = _btnList.indexOf(button);
            if (index != -1)
            {
                return;
            }

            _btnList.push(button);

            button.autoToggle = false;
            button.addEventListener(WxButtonEvent.BUTTON_CLICK, clickButtonHandler);
        }

        /**
         * 移除指定的按钮
         * @param button 要移除的按钮
         */
        public function removeButton(button:WxCheckButton):void
        {
            var index:int = _btnList.indexOf(button);
            if (index == -1)
            {
                return;
            }

            _btnList.splice(index, 1);

            disposeBtnAndData(button);

            if (_currentBtn == button)
            {
                _currentBtn = null;
            }
        }

        /**
         * 清除当前选中的按钮状态
         */
        public function clearStatus():void
        {
            if (_currentBtn != null)
            {
                _currentBtn.selected = false;
            }
            _currentBtn = null;
        }

        /**
         * 移除所有按钮
         */
        public function removeAllButton():void
        {
            while (_btnList.length > 0)
            {
                disposeBtnAndData(_btnList.pop());
            }
        }

        private function disposeBtnAndData(button:WxButton):void
        {
            delete _btnDataDict[button];
            button.removeEventListener(MouseEvent.CLICK, clickButtonHandler);
        }

        private function clickButtonHandler(e:WxButtonEvent):void
        {
            selectButton(e.currentTarget as WxCheckButton);
        }
    }
}
